﻿using System.Xml;
using System.Globalization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Syndication.RSS
{
    public class RSSFeed : FeedBase, IEnumerable<RSSItem>, IFeed
    {

        public override FeedType FeedType { get { return FeedType.RSS; } }
        public override string ContentType { get { return "application/rss+xml"; } }

        public RSSItem[] Items { get { return this.BaseItems.ToArray(typeof(RSSItem)) as RSSItem[]; } }
        public string Description { get { return GetProperty("description", string.Empty) as string; } set { SetProperty("description", value); } }
        public Uri Link { get { return GetProperty("link", null) as Uri; } set { SetProperty("link", value); } }
        public string Copyright { get { return GetProperty("copyright", string.Empty) as string; } set { SetProperty("copyright", value); } }
        public CultureInfo Language { get { return GetProperty("language", null) as CultureInfo; } set { SetProperty("language", value); } }
        public string ManagingEditor { get { return GetProperty("managingEditor", string.Empty) as string; } set { SetProperty("managingEditor", value); } }
        public string WebMaster { get { return GetProperty("webMaster", string.Empty) as string; } set { SetProperty("webMaster", value); } }

        public DateTime LastBuildDate { get { return Convert.ToDateTime(GetProperty("lastBuildDate", DateTime.MinValue)); } set { SetProperty("lastBuildDate", value); } }
        public DateTime PublishDate { get { return Convert.ToDateTime(GetProperty("pubDate", DateTime.MinValue)); } set { SetProperty("pubDate", value); } }
        public string Generator { get { return GetProperty("generator", "Marbles Web Platform") as string; } set { SetProperty("generator", value); } }
        public FeedImage Image { get { return GetProperty("image", null) as FeedImage; } set { SetProperty("image", value); } }

        public RSSFeed()
            : base()
        {
            this.Title = string.Empty;
            this.Description = string.Empty;
            this.Link = null;
            this.Copyright = string.Empty;
            this.Language = CultureInfo.CurrentCulture;
            this.ManagingEditor = string.Empty;
            this.WebMaster = string.Empty;
            this.LastBuildDate = DateTime.Now;
            this.PublishDate = DateTime.Now;
            this.Image = new FeedImage();
        }

        public override string ToString()
        {
            return this.ToXml().InnerXml;
        }

        public override XmlDocument ToXml()
        {
            XmlDocument doc = new XmlDocument();
            XmlNode rss = FeedHelper.CreateElement(doc, "rss");
            XmlNode channel = FeedHelper.CreateElement(doc, "channel");
            rss.Attributes.Append(FeedHelper.CreateAttribute(doc, "version", "2.0"));

            foreach (string s in FeedProperties.Keys)
            {
                if (FeedProperties[s] != null)
                {
                    if (FeedProperties[s].GetType() == typeof(Uri)) { channel.AppendChild(FeedHelper.CreateElement(doc, s, ((Uri)FeedProperties[s]).OriginalString)); }
                    if (FeedProperties[s].GetType() == typeof(CultureInfo)) { channel.AppendChild(FeedHelper.CreateElement(doc, s, ((CultureInfo)FeedProperties[s]).Name)); }
                    if (FeedProperties[s].GetType() == typeof(FeedImage)) { channel.AppendChild(((FeedImage)FeedProperties[s]).ToXml(doc)); }
                    if (FeedProperties[s].GetType() == typeof(DateTime)) { channel.AppendChild(FeedHelper.CreateElement(doc, s, Convert.ToDateTime(FeedProperties[s]).ToString("r"))); }
                    if (FeedProperties[s].GetType() == typeof(string)) { channel.AppendChild(FeedHelper.CreateElement(doc, s, ((string)FeedProperties[s]))); }
                }
            }

            foreach (RSSItem item in this.Items)
            {
                channel.AppendChild(item.ToXml(doc));
            }

            rss.AppendChild(channel);
            doc.AppendChild(rss);
            return doc;
        }

        public int Add(RSSItem item)
        {
            return BaseItems.Add(item);
        }

        public static System.Web.UI.HtmlControls.HtmlLink GetRSSFeedLink(string ID, Uri Url, string Title)
        {
            System.Web.UI.HtmlControls.HtmlLink lnk = new System.Web.UI.HtmlControls.HtmlLink();
            lnk.Href = Url.OriginalString;
            lnk.ID = ID;
            lnk.Attributes.Add("type", "application/rss+xml");
            lnk.Attributes.Add("rel", "alternate");
            lnk.Attributes.Add("title", Title);
            return lnk;
        }


        public IEnumerator<RSSItem> GetEnumerator()
        {
            return BaseItems.GetEnumerator() as IEnumerator<RSSItem>;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Items.GetEnumerator();
        }
    }
}
